<?php

/**
 * 特批发票控制器
 * @author yuzy 2015-5-13
 */

namespace mybackend\controllers;

use common\entities\ConfigEntity;
use Yii;
use common\models\myhtsq\SpecialApprovalAuthorizeModel;
use common\services\myhtsq\SpecialApprovalAuthorizeService;
use common\entities\myhtsq\ActionCode;

class SpecialApprovalAuthorizeController extends BaseController
{

    protected $_authorizeService;

    public function __construct($id, $module, SpecialApprovalAuthorizeService $authorizeService, $config = [])
    {
        $this->_authorizeService = $authorizeService;
        parent::__construct($id, $module, $config);
    }

    protected function roleAuthRules()
    {
        return [
            'actions' => ['GetSpecialApprovalAuthorizeList', 'actionGetSpecialApprovalAuthorizeInfo', 'SaveSpecialApprovalAuthorize', 'ExportSpecialApprovalAuthorizeList','GetPromiseFulfilArealist'],
            'rules' => [
                [
                    'allow' => true,
                    'actionCodeMaps' => [
                        'GetSpecialApprovalAuthorizeList' => ActionCode::SPECIAL_APPROVE_AUTHORIZE_VIEW,
                        'actionGetSpecialApprovalAuthorizeInfo' => ActionCode::SPECIAL_APPROVE_AUTHORIZE_VIEW,
                        'SaveSpecialApprovalAuthorize' => ActionCode::SPECIAL_APPROVE_AUTHORIZE_MNG,
                        'ExportSpecialApprovalAuthorizeList' => ActionCode::SPECIAL_APPROVE_AUTHORIZE_MNG,
                        'GetPromiseFulfilArealist' => ActionCode::SPECIAL_APPROVE_AUTHORIZE_MNG,
                    ]
                ]
            ]
        ];
    }

    /**
     * 特批发票列表
     * @param integer $page 页码
     * @param integer $pageSize 页条数
     * @param string $sortBy 排序字段
     * @param boolean $sortAsc 是否升序
     * @param integer $areaId 区域id
     * @param string $keyword 关键字
     * @return array
     */
    public function actionGetSpecialApprovalAuthorizeList($pageIndex, $pageSize, $sortBy, $sortAsc, $keyword, $areaId, $fulfil)
    {
        $result = $this->_authorizeService->getSpecialApprovalAuthorizeList($pageIndex, $pageSize, $sortBy, $sortAsc, $keyword, $areaId, $fulfil);
        $this->exportJson($result, $this->_authorizeService->getErrCode(), $this->_authorizeService->getErrMsg());
    }

    /**
     * 获取特批发票信息
     * @param string $authorizeId
     * @return \common\models\myhtsq\SpecialApprovalAuthorizeModel
     */
    public function actionGetSpecialApprovalAuthorizeInfo($authorizeId)
    {
        $result = $this->_authorizeService->getSpecialApprovalAuthorizeInfo($authorizeId);
        $this->exportJson($result, $this->_authorizeService->getErrCode(), $this->_authorizeService->getErrMsg());
    }

    /**
     * 保存特批发票
     */
    public function actionSaveSpecialApprovalAuthorize()
    {
        $this->exitIfNotPost();
        $model = new SpecialApprovalAuthorizeModel();
        if ($model->load($_POST['item'], '')) {
            $attachmentList = [];
            if (isset($_POST['attachmentList'])) {
                $attachmentList = $_POST['attachmentList'];
            }
            if ($model->special_approval_authorize_id == '') {
                $_POST['item']['created_on'] = empty($_POST['item']['created_on']) ? ConfigEntity::getDbNowDate() : $_POST['item']['created_on'];
                $result = $this->_authorizeService->addSpecialApprovalAuthorize($model, $attachmentList);
            } else {
                $result = $this->_authorizeService->updateSpecialApprovalAuthorize($model, $attachmentList);
            }

            $this->exportJson($result, $this->_authorizeService->getErrCode(), $this->_authorizeService->getErrMsg());
        } else {
            $this->errorParam();
        }
    }

    /**
     * 导出特批授权
     * @param type $keyword 关键字
     * @param type $areaId 区域
     * @param type $fulfil 是否兑现
     */
    public function actionExportSpecialApprovalAuthorizeList()
    {
        $keyword = $this->getParam('keyword', 'post');
        $areaId = $this->getParam('areaId', 'post');
        $fulfil = $this->getParam('fulfil', 'post');
        $data = $this->_authorizeService->getSpecialApprovalAuthorizeList("", "", "actual_fulfil_time", "desc", $keyword, $areaId, $fulfil);
        $this->_authorizeService->exportSpecialApprovalAuthorizeList($data['items']);
        $this->exportJson($data, $this->_authorizeService->getErrCode(), $this->_authorizeService->getErrMsg());
    }

    /**
     * 获取负责人所在区域
     */
    public function actionGetPromiseFulfilAreaList(){
        $data = $this->_authorizeService->getPromiseFulfilArealist();
        $this->exportJson($data, $this->_authorizeService->getErrCode(), $this->_authorizeService->getErrMsg());
    }

}
